#include "log.hpp"
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

class Requst
{
private:
#define DELIMITER ' '
#define DELIMITER_LEN strlen(DELIMITER)
#define SEP "\r\n"
#define STRLENSEP strlen("\r\n")
public:
    Requst()
    {
    }
    Requst(int x, int y, char op)
        : _x(x), _y(y), _op(op)
    {
    }
    ~Requst()
    {
    }
    // 将请求序列化，返回的应该是字符串
    string Serialize()
    {
        string ret;
        ret += to_string(_x);
        ret += DELIMITER;
        ret += _op;
        ret += DELIMITER;
        ret += to_string(_y);
        return ret;
    }
    // 反序列化，返回值为bool类型表示是否成功反序列化 将直接填充类内成员
    // 自定义序列化为 "123 + 456"
    bool DeSerialize(const string &str)
    {
        size_t left = str.find(DELIMITER);
        if (left == string::npos)
            return false;
        size_t right = str.rfind(DELIMITER);
        if (right == string::npos)
            return false;
        _x = atoi((str.substr(0, left)).c_str());
        _y = atoi((str.substr(right + 1, string::npos)).c_str());
        _op = str[left + 1];
        return true;
    }

public:
    int _x;
    int _y;
    char _op;
};

class Respon
{
public:
    Respon()
    {
    }
    Respon(int result, int code = 0)
        : _result(result), _code(code)
    {
    }
    ~Respon()
    {
    }
    // 将应答序列化，返回的应该是字符串
    string Serialize()
    {
        string ret;
        ret += to_string(_code);
        ret += DELIMITER;
        ret += to_string(_result);
        return ret;
    }
    // 反序列化，返回值为void 直接将填充类内成员
    // 1 123
    bool DeSerialize(const string &str)
    {
        size_t mid = str.find(DELIMITER);
        if (mid == string::npos)
            return false;
        _code = atoi((str.substr(0, mid)).c_str());
        _result = atoi((str.substr(mid + 1, string::npos)).c_str());
        return true;
    }

public:
    int _result;
    int _code;
};

string Recv(int acceptd_sock)
{
    char buffer[1024];
    ssize_t s = recv(acceptd_sock, buffer, sizeof(buffer) - 1, 0);
    if (s < 0)
        logMessage(ERROR, "read error %d %s", errno, strerror(errno));
    else if (s == 0)
    {
        logMessage(ERROR, "服务器关闭\n %d %s", errno, strerror(errno));
    }
    buffer[s] = '\0';
    return buffer;
}
bool Send(int acceptd_sock, string wait_to_send_buffer)
{
    ssize_t s = write(acceptd_sock, wait_to_send_buffer.c_str(), wait_to_send_buffer.size());
    if (s < 0)
    {
        logMessage(ERROR, "write error %d %s", errno, strerror(errno));
        return false;
    }
    else if (s == 0)
    {
        logMessage(NORMAL, "write error %d %s", errno, strerror(errno));
        return false;
    }
    else
    {
        return true;
    }
}
string ita(int a)
{
    if (a == 0)
        return {"0"};
    int flag = 0;
    if (a < 0)
        flag = 1;
    a = abs(a);
    string ret;
    while (a)
    {
        int tem = a % 10;
        char c = tem + '0';
        // cout<<a<<' '<<c<<endl;
        a /= 10;
        ret += {c};
    }
    if (flag == 1)
        ret += '-';
    reverse(ret.begin(), ret.end());
    // cout<<ret<<endl;
    return ret;
}
// lenth/r/n123 + 456/r/n
string Decode(int accepted_sock, string *recvstr)
{
    cout<<"函数内准备接受"<<endl;
    *recvstr += Recv(accepted_sock);
    cout<<"序列化完成，准备编码发送···"<<endl;
    size_t left = (*recvstr).find(SEP);
    if (left == string::npos)
        return "";
    int size = atoi(((*recvstr).substr(0, left)).c_str()); // 拿到有效载荷长度
    //(*recvstr).erase(0,left);//切掉报头
    int remain_size = (*recvstr).size() - left - 2 * STRLENSEP;
    if (remain_size < size)
        return "";
    (*recvstr).erase(0, left);               // 切掉报头
    (*recvstr).erase(0, STRLENSEP);          // 切掉分隔符
    string ret = (*recvstr).substr(0, size); // 拿到有效载荷
    (*recvstr).erase(0, size + STRLENSEP);         // 切掉分隔符
    return ret;
}
// 0 123
string Encode(string *str)
{
    int size = (*str).size();
    (*str) += SEP;
    (*str).insert(0,SEP);
    (*str).insert(0,ita(size));
    return *str;
}